package org.gruposp2p.aularest.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.logging.Logger;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.datatype.XMLGregorianCalendar;
import org.gruposp2p.aularest.server.LinkConstants;
import org.gruposp2p.aularest.utils.AulaLogger;
import org.gruposp2p.aularest.utils.DateUtils;

/**
 *
 * @author jj
 */
@XmlType
@XmlRootElement(name = "student")
@XmlAccessorType(XmlAccessType.NONE)
@Entity
@Table(name = "STUDENT", catalog = "database", schema = "PUBLIC")
@NamedQueries({@NamedQuery(name = "Student.findAll", query = "SELECT s FROM Student s"), @NamedQuery(name = "Student.findById", query = "SELECT s FROM Student s WHERE s.id = :id"), @NamedQuery(name = "Student.findByName", query = "SELECT s FROM Student s WHERE s.name = :name"), @NamedQuery(name = "Student.findBySurnames", query = "SELECT s FROM Student s WHERE s.surnames = :surnames"), @NamedQuery(name = "Student.findByBirthdate", query = "SELECT s FROM Student s WHERE s.birthdate = :birthdate"), @NamedQuery(name = "Student.findByEmail", query = "SELECT s FROM Student s WHERE s.email = :email"), @NamedQuery(name = "Student.findByPhone1", query = "SELECT s FROM Student s WHERE s.phone1 = :phone1"), @NamedQuery(name = "Student.findByPhone2", query = "SELECT s FROM Student s WHERE s.phone2 = :phone2")})
public class Student implements Serializable {
    private static final long serialVersionUID = 1L;

    private static final Logger logger =
        AulaLogger.getLogger(Student.class.getName());

    @Id
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "NAME", nullable = false, length = 48)
    @XmlAttribute(name="name")
    private String name;
    @Basic(optional = false)
    @Column(name = "SURNAMES", nullable = false, length = 48)
    @XmlAttribute(name="surnames")
    private String surnames;
    @Lob
    @Column(name = "PHOTO")
    private byte[] photo;
    @Column(name = "BIRTHDATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date birthdate;
    @Column(name = "EMAIL", length = 48)
    @XmlElement(name="email")
    private String email;
    @Column(name = "PHONE1", length = 48)
    @XmlElement(name="phone1")
    private String phone1;
    @Column(name = "PHONE2", length = 48)
    @XmlElement(name="phone2")
    private String phone2;
    @JoinTable(name = "STUDENT_TUTOR", joinColumns = {@JoinColumn(name = "STUDENT_ID", referencedColumnName = "ID", nullable = false)}, inverseJoinColumns = {@JoinColumn(name = "TUTOR_ID", referencedColumnName = "ID", nullable = false)})
    @ManyToMany
    private Collection<Tutor> tutorCollection;
    @JoinColumn(name = "COURSEGROUP_ID", referencedColumnName = "ID", nullable = false)
    @ManyToOne(optional = false)
    private Coursegroup coursegroupId;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "studentId")
    private Collection<Score> scoreCollection;
    @OneToMany(mappedBy = "studentId")
    private Collection<Address> addressCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "studentId")
    private Collection<Absence> absenceCollection;

    @Transient
    @XmlElement(name="birthdate")
    private XMLGregorianCalendar xmlDate;

    @Transient
    @XmlElement(name="studentLink")
    private Link link;

    public Link getLink() {
        return link;
    }

    public void setLink(Link link) {
        this.name = link.getName();
        this.link = link;
    }
    @Transient
    @XmlElement(name="coursegroupLink")
    private Link coursegroupLink;

    public Link getCoursegroupLink () {
        return coursegroupLink;
    }

    @Transient
    @XmlElement(name="photoLink")
    private Link photoLink;

    @Transient
    @XmlElementWrapper(name = "tutors")
    @XmlElement(name="tutorLink")
    private Collection<Link> tutorsLinks;

    public Collection<Link> getTutorsLinks () {
        return tutorsLinks;
    }

    @Transient
    @XmlElementWrapper(name = "absences")
    @XmlElement(name="absenceLink")
    private Collection<Link> absencesLinks;

    public Collection<Link> getAbsencesLinks () {
        return absencesLinks;
    }

    @Transient
    @XmlElementWrapper(name = "addresses")
    @XmlElement(name="addressLink")
    private Collection<Link> addressesLinks;

    public Collection<Link> getAddressesLinks () {
        return addressesLinks;
    }

    public Student() {
    }

    public Student(Integer id) {
        this.id = id;
    }

    public Student(Integer id, String name, String surnames) {
        this.id = id;
        this.name = name;
        this.surnames = surnames;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurnames() {
        return surnames;
    }

    public void setSurnames(String surnames) {
        this.surnames = surnames;
    }

    public byte[] getPhoto() {
        return photo;
    }

    public void setPhoto(byte[] photo) {
        this.photo = photo;
    }

    public Date getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone1() {
        return phone1;
    }

    public void setPhone1(String phone1) {
        this.phone1 = phone1;
    }

    public String getPhone2() {
        return phone2;
    }

    public void setPhone2(String phone2) {
        this.phone2 = phone2;
    }

    public Collection<Tutor> getTutorCollection() {
        return tutorCollection;
    }

    public void setTutorCollection(Collection<Tutor> tutorCollection) {
        this.tutorCollection = tutorCollection;
    }

    public Coursegroup getCoursegroupId() {
        return coursegroupId;
    }

    public void setCoursegroupId(Coursegroup coursegroupId) {
        this.coursegroupId = coursegroupId;
    }

    public Collection<Score> getScoreCollection() {
        return scoreCollection;
    }

    public void setScoreCollection(Collection<Score> scoreCollection) {
        this.scoreCollection = scoreCollection;
    }

    public Collection<Address> getAddressCollection() {
        return addressCollection;
    }

    public void setAddressCollection(Collection<Address> addressCollection) {
        this.addressCollection = addressCollection;
    }

    public Collection<Absence> getAbsenceCollection() {
        return absenceCollection;
    }

    public void setAbsenceCollection(Collection<Absence> absenceCollection) {
        this.absenceCollection = absenceCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Student)) {
            return false;
        }
        Student other = (Student) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.gruposp2p.restletjpa.model.Student[id=" + id + "]";
    }

    @PostLoad
    public void postProcess() {
        link = new Link();
        link.setHref(LinkConstants.getStudentLink(id.toString()));
        link.setId(id.toString());
        link.setName(name);
        if (photo != null) {
            photoLink = new Link ();
            photoLink.setHref(LinkConstants.getPhotoLink(id.toString()));
        }
        coursegroupLink = coursegroupId.getLink();
        if (birthdate != null) {
            xmlDate = DateUtils.getXMLGregorianCalendar(birthdate);
        }
        if (tutorCollection != null) {
            tutorsLinks = new ArrayList<Link>();
            for (Tutor tutor : tutorCollection) {
                tutorsLinks.add(tutor.getLink());
            }
        }
        if (absenceCollection != null) {
            absencesLinks = new ArrayList<Link>();
            for (Absence absence : absenceCollection) {
                absencesLinks.add(absence.getLink());
            }
        }
        if (addressCollection != null) {
            addressesLinks = new ArrayList<Link>();
            for (Address address : addressCollection) {
                addressesLinks.add(address.getLink());
            }
        }
    }

    @PrePersist
    public void prePersist () {
        this.name = link.getName();
    }

    /**
     * @return the xmlDate
     */
    public XMLGregorianCalendar getXmlDate() {
        return xmlDate;
    }

}
